
<div id="main-wrapper">
	<h1>抽奖详情</h1>
	<ul id="list-viewlottery" class="list-group">
		<li class="list-group-item">
			<span class="li-label">奖项简述</span>
			<p id="content"></p>
		</li>
		<li class="list-group-item">
			<span class="li-label">奖项相关</span>
			<p>
				获奖名额 : 
				<span id="num" class="badge"></span>
			</p>
		</li>
		<li class="list-group-item">
			<span class="li-label">抽奖结果</span>
			<p>得奖名单</p>
			<ul id="list-awardslist" class="list-group"></ul>
		</li>
		<li class="list-group-item">
			<button id="btn-start" class="btn btn-primary btn-lg btn-block">
				开启抽奖
			</button>
		</li>
	</ul>
</div>

<script>
	var params = getHashParams(),
		lyid = parseInt(params['lyid']),
		play = !! params['play'],
		$btnStart = $('#btn-start'),
		$ul = $('#list-awardslist');

	$.get('/do/s/viewlottery?lyid=' + lyid, function(resObj){
		var ok = resObj['ok'], msg = resObj['msg'];
		msg && notify(msg, ok);
		if (! ok) return window.history.back();
		var audienceList = resObj['audienceList'],
			lottery = resObj['lottery'],
			content = lottery['content'],
			num = lottery['num'],
			state = lottery['state'],
			awlist = lottery['awlist'];
		if (state === 'active') {
			$btnStart.attr('disabled', true);
		} else {
			$btnStart.on('click', function(){
				$btnStart.attr('disabled', true);
				$.get('/do/s/startlottery?lyid=' + lyid, function(resObj){
					var ok = resObj['ok'], msg = resObj['msg'];
					msg && notify(msg, ok);
					if (ok) {
						loadFrame('#/viewlottery?lyid=' + lyid
							+ (ok ? '&play=1' : ''));
					} else {
						$btnStart.attr('disabled', false);
					}
				});
			});
		}
		$('#content').text(content);
		$('#num').text(num);

		if (awlist) {
			if (play) {
				$ul.takeoutAwardsList(audienceList, awlist, function(){
					loadFrame('#/viewlottery?lyid=' + lyid);
				});
			} else {
				$ul.setAwardsList(awlist);
			}
		}
	});
</script>
